package io.renren.modules.mark.service.impl;

import io.renren.modules.mark.entity.MarkTeamUserEntity;
import io.renren.modules.mark.service.MarkTeamUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.mark.dao.MarkTeamDao;
import io.renren.modules.mark.entity.MarkTeamEntity;
import io.renren.modules.mark.service.MarkTeamService;


@Service("markTeamService")
public class MarkTeamServiceImpl extends ServiceImpl<MarkTeamDao, MarkTeamEntity> implements MarkTeamService {
    @Autowired
    private MarkTeamUserService markTeamUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx","create_time");
        params.put("order","desc");
        QueryWrapper<MarkTeamEntity> queryWrapper = new QueryWrapper<>();
        Long createUserid = (Long) params.get("createUserid");
        if(createUserid!=null){
            queryWrapper.eq("create_userid",createUserid);
        }
        queryWrapper.eq("is_delete",0);
        IPage<MarkTeamEntity> page = this.page(
                new Query<MarkTeamEntity>().getPage(params),
                queryWrapper
        );
        List<MarkTeamEntity> records = page.getRecords();
        for (MarkTeamEntity record : records) {
            QueryWrapper<MarkTeamUserEntity> qw = new QueryWrapper<>();
            qw.eq("team_id",record.getTeamId());
            record.setUserNum(markTeamUserService.count(qw));
        }

        return new PageUtils(page);
    }

}